package ejb;

import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import repositories.ClientRepository;
import validator.ValidationException;
import validator.ValidationService;
import dbHelper.DBHelper;
import entities.Client;

/**
 * Session Bean implementation class LoginBean
 */
@Stateless
@LocalBean
public class LoginBean implements LoginBeanLocal{

    /**
     * Default constructor. 
     */
    public LoginBean() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public Client getUser(String e_mail) {
		ClientRepository cr = new ClientRepository();
		cr.FindAll();
		//if (cr.ClientList.get(0).getMail() == null) {return null;}
		cr.ClientList.iterator();
		for(Client cl: cr.ClientList) {
			if (e_mail.equals(cl.getMail())) {return cl;}
		}
		return null;
	}

	@Override
	public void addUser(Client cl) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SQLException {
		DBHelper.addObject(cl);		
	}

	@Override
	public boolean validateUser(Client cl) {
		try {
			if (//ValidationService.clientBirthdateValidation(cl.getBirthdate()) &&
				//ValidationService.clientCreditCardValidation(cl.getCreditCard()) &&
				//ValidationService.clientFirstNameValidation(cl.getFirstname()) &&
				//ValidationService.clientGenderValidation(cl.getGender()) &&
				ValidationService.clientMailValidation(cl.getMail()) 
				//ValidationService.clientPasswordValidation(cl.getPassword()) 
				//ValidationService.clientPassportValidation(cl.getPassport()) &&
				//ValidationService.clientPatronymicValidation(cl.getPatronymic())
				) {
				return true;
								
			}
		} catch (ValidationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

}
